package com.neusoft.liduan.d0922;

public class Demo3 {
	/*
	 * 3.银行有一个账户。 有两个储户分别向同一个账户存3000元，每次存1000，存3次。每次存完打印账户余额。
	 * 问题：该程序是否有安全问题，如果有，如何解决？
	 * 
	 */
	public static void main(String[] args) {
		Account a = new Account();
		ChuHu t1 = new ChuHu(a);
		t1.setName("张三");
		t1.start();
		ChuHu t2 = new ChuHu(a);
		t2.setName("李四");
		t2.start();
	}

	private static class Account implements Runnable {
		private int ck;

		public int getCk() {
			return ck;
		}

		public void setCk(int ck) {
			this.ck = ck;
		}

		@Override
		public void run() {
			for (int i = 0; i < 3; i++) {
				synchronized (this) {
					setCk(getCk() + 1000);
					System.out.println(Thread.currentThread().getName() + ":::" + getCk());
					try {
						notify();
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	private static class ChuHu extends Thread {
		private Account account;

		public ChuHu(Account account) {
			this.account = account;
		}

		@Override
		public void run() {
			account.run();
		}
	}
}
